﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Serietheque.Fenetres
{
    /// <summary>
    /// Logique d'interaction pour ModifierEpisode.xaml
    /// </summary>
    public partial class EditEpisodeWindow : Window
    {
        #region Membres privés et public
        private EpisodesTempo m_episode;

        public EpisodesTempo Episode
        {
            get { return m_episode; }
            set { m_episode = value; }
        }
        #endregion Membres privés et public

        public EditEpisodeWindow()
        {
            InitializeComponent();
            m_episode = new EpisodesTempo();
        }

        /// <summary>
        /// Initialisation de la fenetre
        /// </summary>
        /// <param name="_episode">Episode</param>
        public EditEpisodeWindow(EpisodesTempo _episode)
        {
            InitializeComponent();
            m_episode = _episode;
            this.DataContext = Episode;
        }


        private void ok_Click(object sender, RoutedEventArgs e)
        {
            if (skp.BindingGroup.CommitEdit())
            {
                //Tout est ok
                this.DialogResult = true;
                //Update de l'evenement
                skp.BindingGroup.UpdateSources();
                ////Fermeture
                Close();
            }
        }

        private void annuler_Click(object sender, RoutedEventArgs e)
        {
            skp.BindingGroup.CancelEdit();
            this.Close();
        }

        private void skp_Loaded(object sender, RoutedEventArgs e)
        {
            skp.DataContext = m_episode;
            skp.BindingGroup.BeginEdit();
        }

        private void OnCommitBindingGroup(object sender, EventArgs e)
        {
            //skp.BindingGroup.CommitEdit();
        }
    }

    public class VR : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            BindingGroup bindingGroup = value as BindingGroup;
            if (bindingGroup == null)
                return new ValidationResult(false, "VR should only be used with a BindingGroup");

            if (bindingGroup.Items.Count == 1)
            {
                object item = bindingGroup.Items[0];
                EpisodesTempo episode = item as EpisodesTempo;
                if (episode != null && episode.Serie == null)
                    return new ValidationResult(false, "Vous devez écrire un name de série");
            }
            return ValidationResult.ValidResult;
        }
    }
}